package com.iotproject.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.iotproject.eduservice.entity.EduSubject;
import com.iotproject.eduservice.entity.excel.SubjectData;
import com.iotproject.eduservice.service.EduSubjectService;
import com.iotproject.servicebase.exceptionhandler.GlobalException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.Map;

//监听器，用来实现一行一行读取表格数据
public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {
    EduSubjectService eduSubjectService;

    public SubjectExcelListener() {
    }

    public SubjectExcelListener(EduSubjectService eduSubjectService) {
        this.eduSubjectService = eduSubjectService;
    }

    //将读取到的数据写入数据库
    //重点是去重
    //前端开发 vue
    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
        if (subjectData == null) {
            throw new GlobalException(20001, "文件数据为空");
        }
        String oneSubjectName = subjectData.getOneSubjectName();
        String twoSubjectName = subjectData.getTwoSubjectName();
        EduSubject existOneSubject = existOneSubject(eduSubjectService, oneSubjectName);
        if (existOneSubject == null) {//一级目录不存在
            //创建一级目录
            existOneSubject = new EduSubject();
            existOneSubject.setTitle(oneSubjectName);
            existOneSubject.setParentId("0");
            //插入一条记录，此时eduSubject这个对象已经有id值了
            eduSubjectService.save(existOneSubject);
        }
        //获取一级分类id
        String pid = existOneSubject.getId();

        EduSubject existTwoSubject = existTwoSubject(eduSubjectService, twoSubjectName, pid);
        if (existTwoSubject == null) {
            existTwoSubject = new EduSubject();
            existTwoSubject.setTitle(twoSubjectName);
            existTwoSubject.setParentId(pid);
            eduSubjectService.save(existTwoSubject);
        }
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        System.out.println("表头：" + headMap);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    //判断一级目录是否重复
    //可以根据 类别名称title 与 parent_id 父ID判断
    public EduSubject existOneSubject(EduSubjectService subjectService, String title) {
        QueryWrapper<EduSubject> eduSubjectQueryWrapper = new QueryWrapper<>();
        eduSubjectQueryWrapper.eq("title", title);
        eduSubjectQueryWrapper.eq("parent_id", 0);
        EduSubject oneSubject = eduSubjectService.getOne(eduSubjectQueryWrapper);
        return oneSubject;
    }

    //判断二级目录是否重复
    //可以根据类别名称与parent_id 父ID判断
    public EduSubject existTwoSubject(EduSubjectService subjectService, String title, String parentId) {
        QueryWrapper<EduSubject> eduSubjectQueryWrapper = new QueryWrapper<>();
        eduSubjectQueryWrapper.eq("title", title);
        eduSubjectQueryWrapper.eq("parent_id", parentId);
        EduSubject twoSubject = eduSubjectService.getOne(eduSubjectQueryWrapper);
        return twoSubject;
    }

}
